package com.ruoyi.develop.finance.financeBuyInvoice.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.finance.financeBuyInvoice.domain.FinanceBuyInvoice;
import com.ruoyi.develop.finance.financeBuyInvoice.mapper.FinanceBuyInvoiceMapper;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.domain.FinanceBuyInvoiceStoreRel;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.mapper.FinanceBuyInvoiceStoreRelMapper;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.service.IFinanceBuyInvoiceStoreRelService;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.finance.financeBuyInvoice.service.IFinanceBuyInvoiceService;

/**
 * 采购发票Service业务层处理
 *
 * @author lmm
 * @date 2025-07-24
 */
@Service
public class FinanceBuyInvoiceServiceImpl implements IFinanceBuyInvoiceService {
    @Resource
    private FinanceBuyInvoiceMapper financeBuyInvoiceMapper;
    @Resource
    private IFinanceBuyInvoiceStoreRelService financeBuyInvoiceStoreRelService;



    /**
     * 查询采购发票
     *
     * @param id 采购发票主键
     * @return 采购发票
     */
    @Override
    public FinanceBuyInvoice selectById(String id) {
        return financeBuyInvoiceMapper.selectById(id);
    }

    /**
     * 查询采购发票列表
     *
     * @param entity 采购发票
     * @return 采购发票
     */
    @Override
    public List<FinanceBuyInvoice> selectList(FinanceBuyInvoice entity) {
        return financeBuyInvoiceMapper.selectList(entity);
    }

    /**
     * 新增采购发票
     *
     * @param entity 采购发票
     * @return 结果
     */
    @Override
    public int insert(FinanceBuyInvoice entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if (StringUtils.isBlank(entity.getNos())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(entity.getInvoiceDate()==null?new Date():entity.getInvoiceDate());
            String codeMaxString = financeBuyInvoiceMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            entity.setNos(codeMaxString);
        }

        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return financeBuyInvoiceMapper.insert(entity);
    }

    /**
     * 修改采购发票
     *
     * @param entity 采购发票
     * @return 结果
     */
    @Override
    public int update(FinanceBuyInvoice entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        if (StringUtils.isBlank(entity.getNos())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(entity.getInvoiceDate()==null?new Date():entity.getInvoiceDate());
            String codeMaxString = financeBuyInvoiceMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            entity.setNos(codeMaxString);
        }
        return financeBuyInvoiceMapper.update(entity);
    }

    /**
     * 批量删除采购发票
     *
     * @param ids 需要删除的采购发票主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        for (String id: ids){
            FinanceBuyInvoiceStoreRel relPrams = new FinanceBuyInvoiceStoreRel();
            relPrams.setInvoiceId(id);
            List<FinanceBuyInvoiceStoreRel>  relList = financeBuyInvoiceStoreRelService.selectList(relPrams);
            for (FinanceBuyInvoiceStoreRel rel: relList){
                financeBuyInvoiceStoreRelService.deleteById(rel.getId());
            }
        }
        return financeBuyInvoiceMapper.deleteByIds(ids);
    }

    /**
     * 删除采购发票信息
     *
     * @param id 采购发票主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        FinanceBuyInvoiceStoreRel relPrams = new FinanceBuyInvoiceStoreRel();
        relPrams.setInvoiceId(id);
        List<FinanceBuyInvoiceStoreRel>  relList = financeBuyInvoiceStoreRelService.selectList(relPrams);
        for (FinanceBuyInvoiceStoreRel rel: relList){
            financeBuyInvoiceStoreRelService.deleteById(rel.getId());
        }
        return financeBuyInvoiceMapper.deleteById(id);
    }
}
